package com.ruoyi.common.utils;

import com.ruoyi.common.exception.ServiceException;
import org.apache.commons.lang3.ObjectUtils;

/**
 * 断言工具类
 */
public class Assert {

    public static void equals(Object obj1, Object obj2, String message) {
        equals(obj1, obj2, () -> new ServiceException(message));
    }

    public static void equals(Object obj1, Object obj2, CreateException error) {
        if (ObjectUtils.notEqual(obj1, obj2))
            newError(error);
    }

    public static void notEquals(Object obj1, Object obj2, String message) {
        notEquals(obj1, obj2, () -> new ServiceException(message));
    }

    public static void notEquals(Object obj1, Object obj2, CreateException error) {
        if (!ObjectUtils.notEqual(obj1, obj2))
            newError(error);
    }

    public static void Null(Object obj, String message) {
        Null(obj, () -> new ServiceException(message));
    }

    public static void Null(Object obj, CreateException message) {
        if (obj != null)
            newError(message);
    }

    public static void notNull(Object obj, String message) {
        notNull(obj, () -> new ServiceException(message));
    }

    public static void notNull(Object obj, CreateException message) {
        if (obj == null)
            newError(message);
    }

    public static void notEmpty(Object obj, String message) {
        notEmpty(obj, () -> new ServiceException(message));
    }

    public static void notEmpty(Object obj, CreateException message) {
        if (ObjectUtils.isEmpty(obj))
            newError(message);
    }

    private static void newError(CreateException error) {
        throw error.error();
    }

    @FunctionalInterface
    public static interface CreateException {
        RuntimeException error();
    }

    public static void isTrue(boolean expression, String message) {
        isTrue(expression, () -> new ServiceException(message));
    }

    public static void isTrue(boolean expression, CreateException message) {
        if (!expression)
            newError(message);
    }
}
